﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SmartSpace.Documents.Services;
using SmartSpace.Web;

namespace SmartSpace.Documents.UI.Pages
{
    public partial class FileList : SmartSpace.Web.SsPart
    {

        public FileList()
            : base(new Guid("{0171BB47-BFC3-417B-A63A-29279520F555}"))
        {
        }


        public string doc_Id = "";

        protected override void OnLoad(object sender,EventArgs e)
        {
            this.RegisterJs("common", "../Resource/JavaScript/common.js");
            this.RegisterCss("site", "../Resource/Style/site.css");

            String folder_Id = Request["id"] ?? String.Empty;

            Guid folderId = Guid.NewGuid();

            if (!IsPostBack)
            {
                DocServices services = new DocServices();
                DataTable dt = services.GetAllFolders();
                Bind_Tv(dt, _tree_view.Nodes, "0", "Id", "ParentId", "FolderName");
                if (folder_Id != "")
                {
                    Guid guid = new Guid(folder_Id);
                    BindRepeater(guid);
                }
            }
        }

        private void Bind_Tv(DataTable dt, TreeNodeCollection tnc, string pid_val, string id, string pid, string text)
        {
            string filter = string.Format("{0}={1}", pid, pid_val);
            DataRow[] rows = dt.Select(filter);

            TreeNode tn;                                            //建立TreeView的节点（TreeNode），以便将取出的数据添加到节点中

            foreach (DataRow drv in rows)
            {
                tn = new TreeNode();                                //建立一个新节点
                tn.Value = drv[id].ToString();                      //节点的Value值，一般为数据库的id值
                tn.Text = drv[text].ToString();                     //节点的Text，节点的文本显示
                tn.ImageUrl = "img.png";
                tn.NavigateUrl = "?id=" + drv[id].ToString();
                tnc.Add(tn);                                        //将该节点加入到TreeNodeCollection（节点集合）中
                Bind_Tv(dt, tn.ChildNodes, tn.Value, id, pid, text);//递归（反复调用这个方法，直到把数据取完为止）
            }
        }

        public void BindRepeater(Guid id)
        {
            DocServices services = new DocServices();
            List<Document> list = services.GetDocsByFolderId(id);
            rep1.DataSource = list;
            rep1.DataBind();
        }

        public String ConvertImage(string val)
        {
            return "";
        }

        protected void rep1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            doc_Id = e.CommandArgument.ToString();
            Guid did = new Guid (doc_Id);
            if (e.CommandName == "Delete")
            {
                DocServices services = new DocServices();
                services.DeleteDocument(did);
            }
        }
    }
}